﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;

namespace ProductShop
{
    public partial class FormProductShop : Form, IProductShopView
    {
        public event Action ProductCategoryChanged;
        public event Action ParticularProductSelected;
        public event Action ProductDeleted;
        public event Action ProductAdded;
        public event Action ProductChanged;

        public IEnumerable<string> ProductCategoryNames
        {
            get
            {
                return (IEnumerable<string>)cmbProductCategories.Items;
            }
            set
            {
                cmbProductCategories.DataSource = value;
                cmbProductCategories.SelectedIndex = 0;
            }
        }
        public IEnumerable ProductData
        {
            get
            {
                return (IEnumerable)dgvProducts.DataSource;
            }
            set
            {
                dgvProducts.DataSource = value;
            }
        }
        public Image SelectedProductImage
        {
            get
            {
                return pbProductImage.Image;
            }
            set
            {
                pbProductImage.Image = value;
            }
        }
        public String SelectedProductID
        {
            get
            {
                return tbProductId.Text;
            }
            set
            {
                tbProductId.Text = value;
            }
        }
        public String SelectedProductName
        {
            get
            {
                return tbProductName.Text;
            }
            set
            {
                tbProductName.Text = value;
            }
        }
        public String SelectedProductPrice
        {
            get
            {
                return tbProductPrice.Text;
            }
            set
            {
                tbProductPrice.Text = value;
            }
        }
        public String SelectedProductDescription
        {
            get
            {
                return lbProductDescription.Text;
            }
            set
            {
                if (value != null)
                    lbProductDescription.Items.Add(value);
            }
        }

        public FormProductShop()
        {
            InitializeComponent();
            dgvProducts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvProducts.MultiSelect = false;
            cmbProductCategories.SelectedIndexChanged += (sender, e) => InvokeEvent(ProductCategoryChanged);
            dgvProducts.SelectionChanged += (sender, e) => InvokeEvent(ParticularProductSelected);
            btnDelete.Click += (sender, e) => InvokeEvent(ProductDeleted);
        }

        private void InvokeEvent(Action action)
        {
            if (action != null) action();
        }

        public String GetSelectedProductCategory()
        {
            return cmbProductCategories.SelectedItem.ToString();
        }

        public string GetSelectedProductID()
        {
            return dgvProducts.SelectedRows.Count == 0 ? null : dgvProducts.SelectedRows[0].Cells["ProductId"].Value.ToString();
        }

        public void ShowForm()
        {
            Show();
        }

        public void CloseForm()
        {
            Close();
        }
    }
}
